package com.example.schoolenterprisealliance.fragment;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.schoolenterprisealliance.R;
import com.example.schoolenterprisealliance.activity.ItemActivity;
import com.example.schoolenterprisealliance.activity.PolicyActivity;
import com.example.schoolenterprisealliance.adapter.PolicyAdapter;
import com.example.schoolenterprisealliance.bean.Policy;
import com.example.schoolenterprisealliance.io.AppConfig;
import com.example.schoolenterprisealliance.service.PolicyService;

import java.util.List;

public class PolicyFragment extends Fragment {
    private View view;
    private PolicyAdapter policyAdapter;
    private RecyclerView recyclerView;
    private TextView tv_title;
    private TextView tv_context;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_policy, container, false);
        recyclerView=view.findViewById(R.id.rv_poblic);
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));
        View view1 = LayoutInflater.from(getActivity()).inflate(R.layout.item_information,null);
        tv_title=view1.findViewById(R.id.tv_title);
        tv_context=view1.findViewById(R.id.tv_context);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        policyData();
        return view;
    }
    //获取政策数据
    private void policyData() {
        //1, 声明Retrofit 的构建者
        Retrofit.Builder builder = new Retrofit.Builder();
        //2, 设置公共的地址
        builder.baseUrl(AppConfig.BASE_URL);
        //3, 设置解析gson
        builder.addConverterFactory(GsonConverterFactory.create());
        //4, 得到Retrofit 对象
        Retrofit retrofit = builder.build();
        //5, 得到接口
        PolicyService policyService = retrofit.create(PolicyService.class);
        //6, 得到Call 的方法
        Call<Policy> call = policyService.getPolicyData();
        //7, 传入接口回调 , 获取数据
        call.enqueue(new Callback<Policy>() {
            @Override
            public void onResponse(Call<Policy> call, Response<Policy> response) {
                Policy policy=response.body();
                //数据源
                List<Policy.DataBean> data = policy.getData();
                PolicyAdapter policyAdapter = new PolicyAdapter(R.layout.item_policy,data);
                recyclerView.setAdapter(policyAdapter);
                policyAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        Intent intent=new Intent(getActivity(), ItemActivity.class);
                        intent.putExtra("title",data.get(position).getTitle());
                        intent.putExtra("time",data.get(position).getTime());
                        intent.putExtra("context",data.get(position).getContext());
                        intent.putExtra("layout","policy");
                        tv_title.setTextColor(Color.parseColor("#FF0000"));
                        tv_context.setTextColor(Color.parseColor("#FF0000"));
                        tv_title.setText("sdfsf");
                        recyclerView.setAdapter(policyAdapter);
                        Log.e("position",position+"");
                        startActivity(intent);
                    }
                });
            }

            @Override
            public void onFailure(Call<Policy> call, Throwable t) {
                t.printStackTrace();
            }


        });
    }
}
